#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#--------------------------------------------------------------------------------------------------------------------#
#--------------------------------------------------- Main models ----------------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------#

rm(list=ls())
setwd(".../...")
load("d_nr15_cand")
load("d_nr15_cand_m")
load("d_nr15_cand_n")
load("d_nr15_cand_p")

# ------------------------------------------------------------------ Table 3: Estimating the relationship between a candidate's name (Swiss vs. non-Swiss) and negative preference votes

# ----- Estimate main models

m1 <- lme(npv_rel ~ nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

m2 <- lme(npv_rel ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + num_listpos_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

m3 <- lme(npv_rel ~ nonswissname + partypos + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + num_listpos_c + partypos*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)

# ----- Check multicollinearity

vif.lme(m3)

# ----- Table

custom.name <- c("Intercept", "Non-Swiss name", 
"Relative list ranking", "Incumbent",
"Pre-cumulated", "Male", "Age: 30-50 years ", "Age: 50+ years", 
"Party position = center", "Party position = left",
"Number of candidates on party list",
"Non-Swiss name*Party position = center", 
"Non-Swiss name*Party position = left")

texreg(list(m1, m2, m3), booktabs=TRUE,
caption="Estimating the relationship between a candidate's name (Swiss vs. non-Swiss) and negative preference votes",
omit.coef = "bfscantno",
custom.coef.names = custom.name,
reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1),
caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

# ------------------------------------------------------------------ Figure 2: Interaction effect of candidate name (Swiss, Non-Swiss) and party position on negative preference votes

d_nr15_cand_n$partypos <- mapvalues(d_nr15_cand_n$partypos, 
from = c(1, 2, 3), 
to = c(3, 2, 1))

m3a <- lme(npv_rel ~ nonswissname + partypos + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 +
partypos + num_listpos_c + partypos*nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_n)
summary(m3a)

f_negint <- plot_model(m3a, type = "eff", terms = c("partypos", "nonswissname"), 
colors = c("black", "grey55"), 
title = "",
legend.title = "Candidate name", 
axis.title = c("Ideological position of parties", "Negative preference votes")) + 
theme_bw() +
scale_colour_manual(values = c("black", "grey")) +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank()) + 
#ylim(0.6, 1.2) +
annotate("text", x=1.3, y=1.02, label = "Left", vjust=1, hjust=1, size = 3.3) +
annotate("text", x=2.3, y=1.05, label = "Center", vjust=1, hjust=1, size = 3.3) + 
annotate("text", x=3.3, y=1.12, label = "Right", vjust=1, hjust=1, size=3.3); f_negint

png("f_negint.png", width = 8.5, height = 4.5, units = 'in', res = 300)
plot(f_negint)
dev.off()

# ------------------------------------------------------------------ Table 4: Estimating the relationship between a candidate's name (Swiss vs. non-Swiss) and positive preference votes

# ----- Estimate models (linear)

m1 <- lme(pos_mean ~ nonswissname + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m)

m2 <- lme(pos_mean ~ nonswissname + rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + partypos + num_listpos_c + bfscantno.x, random = ~ 1 | list_id, data=d_nr15_cand_m)

d_nr15_cand_m$partypos <- factor(d_nr15_cand_m$partypos)

m3 <- lme(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + num_listpos_c + bfscantno.x + partypos*nonswissname, random = ~ 1 | list_id, data=d_nr15_cand_m)

# ----- Check multicollinearity

vif.lme(m3)

# ----- Table

custom.name <- c("Intercept", "Non-Swiss name", 
"Relative list ranking", "Incumbent",
"Pre-cumulated", "Male", "Age: 30-50 years ", "Age: 50+ years", 
"Party position = centre", "Party position = left",
"Number of candidates on party list",
"Non-Swiss name*Party position = centre", 
"Non-Swiss name*Party position = left")

texreg(list(m1, m2, m3), booktabs=TRUE,
       caption="Positive preference votes vs. non-Swiss name",
       omit.coef = "bfscantno",
       custom.coef.names = custom.name,
       reorder.coef = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1),
       caption.above = TRUE, stars = c(0.001, 0.01, 0.05, 0.1))

# ------------------------------------------------------------------ Figure 3: Interaction effect of candidate name (Swiss, Non-Swiss) and party position on positive preference votes

d_nr15_cand_m$partypos <- mapvalues(d_nr15_cand_m$partypos, 
from = c(1, 2, 3), 
to = c(3, 2, 1))

m3a <- lme(pos_mean ~ rel_pos_c + bfsincumbency + precumulated + bfssex + age_c3 + num_listpos_c + bfscantno.x + partypos*nonswissname, random = ~ 1 | list_id, data=d_nr15_cand_m)

ef <- effect("partypos:nonswissname", m3a)
summary(ef)
ef <- as.data.frame(ef)
f_posint <- ggplot(ef, aes(partypos, fit, color=nonswissname)) + geom_point(size=2, position = position_dodge(width = 0.4)) + 
geom_errorbar(aes(ymin=lower, ymax=upper), width=0.2, size=1, position = position_dodge(width = 0.4)) + 
theme_bw() +
scale_colour_manual(values = c("black", "grey")) +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank()) + 
annotate("text", x=1.4, y=1.05, label = "Left", vjust=1, hjust=1, size = 3.3) +
annotate("text", x=2.4, y=1.1, label = "Center", vjust=1, hjust=1, size = 3.3) + 
annotate("text", x=3.4, y=1.02, label = "Right", vjust=1, hjust=1, size=3.3) +
labs(x="Ideological position of parties", y="Positive preference votes", color="Candidate name") + 
ggtitle(""); f_posint

png("f_posint.png", width = 8.5, height = 4.5, units = 'in', res = 300)
plot(f_posint)
dev.off()
